package ForkJoinDemo;

import java.util.concurrent.RecursiveTask;

public class ForkJoinCalculate extends RecursiveTask<Long> {

    private long start;
    private long end;


    private static final long THRESHOLD=10000;

    public ForkJoinCalculate(long start, long end) {
        this.start = start;
        this.end = end;
    }

    @Override
    protected Long compute() {
        long length = end - start;
        long sum=0;
        if(length<=THRESHOLD){ //小于这个阈值，任务就不再拆分了。

            for (long i=start;i<=end;i++){
                sum+=i;
            }
            return sum;
        }else{
            long middle=(start+end)/2;
            ForkJoinCalculate left = new ForkJoinCalculate(start, middle);
            left.fork(); //拆分
            ForkJoinCalculate right= new ForkJoinCalculate(middle+1,end);
            right.fork(); //拆分
            return left.join()+right.join();
        }
    }
}
